package com.yiqi.zhiyuan.feature.certification.view;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.luck.picture.lib.entity.EventEntity;
import com.luck.picture.lib.rxbus2.RxBus;
import com.luck.picture.lib.rxbus2.Subscribe;
import com.luck.picture.lib.rxbus2.ThreadMode;
import com.suozhang.framework.component.http.ApiManager;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.widget.PowerfulEditText;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.entity.dto.DiverCertificationDTO;
import com.yiqi.zhiyuan.entity.vo.CertufucateInfoVO;
import com.yiqi.zhiyuan.feature.certification.contract.CertificationContract;
import com.yiqi.zhiyuan.feature.certification.dependencies.certification.CertificationPresenterModule;
import com.yiqi.zhiyuan.feature.certification.dependencies.certification.DaggerCertificationComponent;
import com.yiqi.zhiyuan.feature.main.view.MainActivity;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.yiqi.zhiyuan.framework.ZYConstants.CertificateSuccess;
import static com.yiqi.zhiyuan.framework.ZYConstants.ORDER_JIXU;
import static com.yiqi.zhiyuan.framework.ZYConstants.ORDER_SHOUCHE;

/**
 * @ProjectName: ZhiYuan
 * @Package: com.yiqi.zhiyuan.feature.login.view
 * @ClassName: UserInformationActivity
 * @Description: java类作用描述
 * @Author:
 * @CreateDate: 2019/3/11 17:45
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/11 17:45
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class UserInformationActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener, CertificationContract.View, TextWatcher {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.edt_user_name)
    PowerfulEditText mEdtUserName;
    @BindView(R.id.edt_id_number)
    PowerfulEditText mEdtIdNumber;
    @BindView(R.id.edt_car_number)
    PowerfulEditText mEdtCarNumber;
    @BindView(R.id.edt_bank_number)
    PowerfulEditText mEdtBankNumber;
    @BindView(R.id.edt_car_brand_number)
    PowerfulEditText mEdtCarBrandNumber;
    @BindView(R.id.edt_car_brand)
    TextView mEdtCarBrand;
    @BindView(R.id.edt_car_model)
    TextView mEdtCarModel;
    @BindView(R.id.edt_car_color)
    TextView mEdtCarColor;
    @BindView(R.id.tv_user_submit)
    TextView mTvUserSubmit;
    @BindView(R.id.tab_certificate_register)
    TextView mTabCertificateRegister;
    @BindView(R.id.tab_certificate_info)
    TextView mTabCertificateInfo;
    @BindView(R.id.tab_certificate_image)
    TextView mTabCertificateImage;
    @BindView(R.id.rg_sex)
    RadioGroup mRgSex;

    private boolean isFirst = false;
    private Integer isPass;

    private String mBrandStr;
    private String mModelStr;
    @Inject
    CertificationContract.Presenter mPresenter;

    CertufucateInfoVO mCertufucateInfoVO;

    public static final int CAR_MODEL = 0x001;
    public static final int CAR_COLOR = 0x002;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_user_information;
    }

    @Override
    protected void initInjector() {
        DaggerCertificationComponent.builder().certificationPresenterModule(new CertificationPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        RxBus.getDefault().register(this);
        initToolBar(mToolbar, getString(R.string.user_data_submit_title), true, true);
    }

    @Override
    protected void initData() {

        isFirst = this.getIntent().getBooleanExtra("is_first", false);
        isPass = this.getIntent().getIntExtra("auth_state", -1);
        initTabView(isFirst);

        if (isPass == 0 || isPass == 2 || isPass == 3) {
            mPresenter.queryAuthInfo();
        }
    }

    /**
     * 初始化头部选择按钮
     */
    private void initTabView(boolean isCer) {
        if (isCer) {
            mToolbar.inflateMenu(R.menu.menu_skip);
        } else {
            mTabCertificateRegister.setText("填写资料");
            mTabCertificateInfo.setVisibility(View.GONE);

        }
    }

    @Override
    protected void initEvent() {
        mToolbar.setOnMenuItemClickListener(this);
        mEdtUserName.addTextChangedListener(this);
        mEdtIdNumber.addTextChangedListener(this);
        mEdtCarNumber.addTextChangedListener(this);
        mEdtBankNumber.addTextChangedListener(this);
        mEdtCarBrandNumber.addTextChangedListener(this);
    }

    @OnClick({R.id.edt_car_brand, R.id.edt_car_model, R.id.edt_car_color})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.edt_car_brand:
                break;
            case R.id.edt_car_model:
                Intent colorModel = new Intent(this, CityPickerActivity.class);
                startActivityForResult(colorModel, CAR_MODEL);
                break;
            case R.id.edt_car_color:
                Intent colorIntent = new Intent(this, SelectColorActivity.class);
                startActivityForResult(colorIntent, CAR_COLOR);
                break;
            default:
                break;

        }
    }

    /**
     * 提交资料
     */
    @OnClick(R.id.tv_user_submit)
    public void onViewSubmitClicked() {
        mPresenter.uploadInfo(
                mEdtUserName.getText().toString().trim(),
                mRgSex.getCheckedRadioButtonId() == R.id.rb_sex_man ? getString(R.string.text_user_man) : getString(R.string.text_user_women),
                mEdtIdNumber.getText().toString().trim(),
                mEdtCarNumber.getText().toString().trim(),
                mEdtBankNumber.getText().toString().trim(),
                mEdtCarBrandNumber.getText().toString().trim(),
                mBrandStr,
                mModelStr,
                mEdtCarColor.getText().toString().trim()
        );
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        startActivity(new Intent(this, MainActivity.class));
        finish();
        return false;
    }


    @Override
    public void showUploadInfoSuccess(DiverCertificationDTO certificationDTO) {
        Intent intent = new Intent(this, UploadCertificatesActivity.class);
        intent.putExtra("is_first", isFirst);
        intent.putExtra("auth_state", isPass);
        intent.putExtra("certificate_info", certificationDTO);
        if (!isFirst && mCertufucateInfoVO != null) {
            intent.putExtra("auth_images", mCertufucateInfoVO.getReviewVo());
        }

        startActivity(intent);
        overridePendingTransition(0, 0);
    }

    @Override
    public void showUploadInfoError(String errorMsg) {
        showMsg(errorMsg);
    }

    @Override
    public void showAuthInfoSuccess(CertufucateInfoVO s) {
        mCertufucateInfoVO = s;
        if (s.getFullInVo() != null) {
            mEdtUserName.setText(s.getFullInVo().getRealName());
            mRgSex.check(s.getFullInVo().getSex().equals(R.string.text_user_man) ? R.id.rb_sex_man : R.id.rb_sex_woman);
            mEdtIdNumber.setText(s.getFullInVo().getIdCard());
            mEdtCarNumber.setText(s.getFullInVo().getDriverLicense());
            mEdtBankNumber.setText(s.getFullInVo().getBankCard());
            mEdtCarBrandNumber.setText(s.getFullInVo().getCarNo());
            mBrandStr = s.getFullInVo().getCarName();
            mModelStr = s.getFullInVo().getCarModelNo();
            mEdtCarModel.setText(s.getFullInVo().getCarName() + "-" + s.getFullInVo().getCarModelNo());
            mEdtCarColor.setText(s.getFullInVo().getCarColor());
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(mEdtUserName.getText().toString().trim()) &&
                !TextUtils.isEmpty(mEdtIdNumber.getText().toString().trim()) &&
                !TextUtils.isEmpty(mEdtCarNumber.getText().toString().trim()) &&
                !TextUtils.isEmpty(mEdtBankNumber.getText().toString().trim()) &&
                !TextUtils.isEmpty(mEdtCarBrandNumber.getText().toString().trim())) {
            mTvUserSubmit.setEnabled(true);

        } else {
            mTvUserSubmit.setEnabled(false);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CAR_MODEL:
                if (data != null) {
                    mBrandStr = data.getStringExtra("brand");
                    mModelStr = data.getStringExtra("model");
                    mEdtCarModel.setText(mBrandStr + "-" + mModelStr);
                }
                break;

            case CAR_COLOR:
                if (data!=null){
                    String color = data.getStringExtra("color");
                    mEdtCarColor.setText(color);
                }
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventBus(EventEntity obj) {
        switch (obj.what) {
            case CertificateSuccess:
               finish();
               AM.appManager().popActivity(this);
                break;

        }
    }
}
